<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;
use Think\Model;

class BusinessserviceController extends AdminbaseController{
    
	protected $businessServiceModel;
	
	public function _initialize() {
		parent::_initialize();
		$this->businessServiceModel = M("BusinessService");
	}

    //商户服务列表
	public function index(){
        $id=I('id',0,'intval');
        $type=I('type',0,'intval');
        $region_id=I('region_id',0,'intval');
        $business_name=I('business_name');
        $name=I('name');
        if($name){
            $where['bs.name']=$name;
        }
        if($business_name){
            $where['b.name']=$business_name;
        }
        if($type){
            $where['b.type']=$type;
        }
        if($id){
            $where['bs.service_id']=$id;
        }
        if($region_id){
            $where['b.region_id']=$region_id;
        }
        $where['bs.is_deleted']=0;
        $where['b.status']=1;
        $where['b.is_checked']=1;
        //分页
        $count=M('BusinessService')
            ->alias('bs')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bs.business_id')
            ->where($where)
            ->count();
        $page = $this->page($count, 20);
        //查找商户服务,排序
        $services=M('BusinessService')
            ->alias('bs')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bs.business_id')
            ->field('bs.id,bs.name,bs.price,bs.good_at,bs.order_number,b.name as business_name,bs.create_time,bs.service_id,true_order_number')
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order('bs.create_time')
            ->select();
        foreach ($services as &$service){
            $s=M('Service')->where(['id'=>$service['service_id']])->find();
            if($s['pid']==0){
                $service['service_name']=$s['name'];
            }else{
                $e=M('Service')->where(['id'=>$s['pid']])->find();
                $service['service_name']=$e['name'].'-'.$s['name'];
            }
        }
        $this->assign("services",$services);
        $this->assign("page", $page->show('Admin'));
        $this->display();
	}

    //活动详情
    public function getDetails(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $content=$this->businessServiceModel
            ->alias('bs')
            ->join('LEFT JOIN __BUSINESS_SERVICE_IMAGE__ as bsi ON bsi.business_service_id=bs.id')
            ->where(['bs.id'=>$id,'bs.is_deleted'=>0])
            ->field('bs.content,bsi.image')
            ->select();
        $this->assign("content",$content);
        $this->display('getDetails');
    }

    //获取子分类
    public function getChildrenServices(){
        $id=I('id',0,'intval');
        $model=A("App/Businessservice");
        $result=$model->getChildrenServices($id);
        $this->ajaxReturn(['status'=>'success','data'=>$result]);
    }

    //增加订单数量
    public function addOrderNumber(){
        $id=I('id',0,'intval');
        $number=I('number',0,'intval');
        if(!$id||!$number||$number<0){
            $this->error('数量不正确');
        }
        $service=$this->businessServiceModel->where(['id'=>$id,'is_deleted'=>0])->find();
        if($service){
            $result=$this->businessServiceModel->where(['id'=>$id])->setInc('order_number',$number);
            if($result!==false){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->error('服务不存在');
        }
    }
}